package com.zerox.controller;

import com.alibaba.fastjson.JSON;
import com.zerox.entity.SysRole;
import com.zerox.service.SysRoleService;
import com.zerox.uilt.UserListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/role", produces = {"application/json;charset =utf-8"})
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    @GetMapping("toRoleList")
    public String role() {
        return "role/role";
    }

    @RequestMapping(value = "/role2", method = RequestMethod.GET)
    @ResponseBody
    public String roleList(Integer page, Integer limit, @RequestParam(value = "roleName", required = false) String roleName) {

        List<SysRole> roleList = sysRoleService.listRole(page, limit, roleName);
        int count = sysRoleService.count();

        UserListVO userListVO = new UserListVO(0, count, roleList, "");

        return JSON.toJSONString(userListVO);
    }


    @GetMapping("toadd")
    public String add() {
        return "role/add";
    }

    @PostMapping("add")
    @ResponseBody
    public String add2(SysRole sysRole) {

        int row = sysRoleService.add(sysRole);

        return row > 1 ? "1" : "0";
    }


    @PostMapping("delete/{roleId}")
    @ResponseBody
    public String delete(@PathVariable("roleId") Integer roleId) {
        int row  = sysRoleService.delete(roleId);

        return row > 1 ? "1" : "0";
    }


    @GetMapping("toedit/{roleId}")
    public String toedit(@PathVariable("roleId") Integer roleId, HttpServletRequest request) {
        SysRole sysRole = sysRoleService.selectById(roleId);
        request.setAttribute("sysRole",sysRole);
        return "role/edit";
    }

    @PostMapping("update")
    @ResponseBody
    public String update(SysRole sysRole) {
        int row = sysRoleService.update(sysRole);

        return row >0 ? "1" : "0";
    }


}
